<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2023/6/24
 * Time: 9:43
 */

namespace app\index\model;


use think\Model;

class CrmChannelCount extends Model
{
    public function count_list($query){
        if(!empty($query['goods_next_id'])){
            $where['c.goods_next_id']=$query['goods_next_id'];
        }
        if(!empty($query['starttime']) && !empty($query['endtime'])){
            if($query['starttime']>$query['endtime'])apiReturn(400,'查询开始日期不能大于结束日期');
            $where['c.time']=['between',[strtotime($query['starttime']),strtotime($query['endtime'])]];
        }
        if(!empty($query['course_id'])){
            $where['gn.goods_id']=$query['course_id'];
        }
        if(isset($where)){
                $res = $this->s_where($where)->field('c.*,gn.title as goods_next_title,gn.goods_id,g.title as goods_title')->order('c.time asc')->limit($query['limit'])->page($query['page'])->select();
                $count =  $this->s_where($where)->count();
        }else{
            $res = $this->alias('c')->join('goods_next gn','c.goods_next_id=gn.id')->join('goods g','gn.goods_id=g.id')->field('c.*,gn.title as goods_next_title,gn.goods_id,g.title as goods_title')->order('c.time asc')->limit($query['limit'])->page($query['page'])->select();
            $count = $this->alias('c')->count();
        }
        return ['list'=>$res,'count'=>$count];
    }

    private function s_where($where){
        return $this->alias('c')->join('goods_next gn','c.goods_next_id=gn.id')->join('goods g','gn.goods_id=g.id')->where($where);
    }
}